package com.ibeacon.controller.common;


import com.ibeacon.model.AccessToken;

public class TokenThread implements Runnable {    
	Log log = Log.getLogger();
	// 第三方用户唯一凭证    
    public static String appid = "";    
    // 第三方用户唯一凭证密钥    
    public static String appsecret = "";    
    public static AccessToken accessToken = null;    
    
    public void run() {    
        while (true) {
            try {    
                accessToken = WeixinUtil.getAccessToken(appid, appsecret);    
                if (null != accessToken) {    
                	log.logger.info("获取access_token成功，有效时长{}秒 token:{}"+accessToken.getExpiresIn()+accessToken.getToken());    
                    // 休眠7000秒    
                    Thread.sleep((accessToken.getExpiresIn() - 200) * 1000);    
                } else {    
                    //如果access_token为null，60秒后再获取    
                    Thread.sleep(60 * 1000);    
                }    
            } catch (InterruptedException e) {    
                try {
                    Thread.sleep(60 * 1000);    
                } catch (InterruptedException e1) {
                	log.logger.error("{}", e1);    
                }    
                log.logger.error("{}", e);    
            }    
        }    
    }    
}    